<?php

/***********************************************************
 * TODO:  detail,edit,delete,page.
 * 
 * 還有 MISC ?
 *  
 ***********************************************************/
class yswarranty_ctl_site_mywarranty extends b2c_ctl_site_member
{
   function __construct( &$app )
   {
      $this->app_current = $app;
      $this->app_b2c = app::get( "b2c" );
      parent::__construct($this->app_b2c);
      //parent::__construct($app);
   }

   public function get_channelopt()
   {
      $opt=  array('1'=>'1.網(郵)購','2'=>'2.量販店(或大賣場)','3'=>'3.電器行','4'=>'4.TV購','5'=>'5.其他');
      return $opt;
   }

   public function warranty_policy($app = "", $ctl = "", $act = "")
   {
      $this->begin( $this->gen_url( array(
                  "app" => $app,
                  "ctl" => $ctl,
                  "act" => $act
      ) ) );

      $arr_settings = array( );

      $this->pagedata['is_open_yswarranty'] = $this->app->getConf( "site.is_open_yswarranty" );
      $this->pagedata['comment'] = app::get( "yswarranty" )->getConf( "site.yswarranty_comment" );
      $this->pagedata['args'] = array(
                  $app,
                  $ctl,
                  $act
      );
      //$this->pagination( 1, 1, "indexx", "", "yswarranty", "site_mywarranty" );

      $this->output("yswarranty");
   }
   public function valid_warranty_no($warranty_no)
   {
      return TRUE;
   }
    
    
    
   public function checkBeforePost($url)
   {
      $chkcode=$_POST['askverifyCode'];
      if ( !base_vcode::verify( "ASKVCODE", intval( $chkcode ) ) )
      {
         $this->splash( "failed", $url, app::get( "yswarranty" )->_( "驗證碼錯誤" ) );
         return;
      }
   }
    
   public function check_warranty_input()
   {
      $warranty_no=trim($_POST['warranty_no']);
      if(empty($warranty_no))
      {
         //splash就離開
         $this->splash( "failed", $url, app::get( "yswarranty" )->_( "保證書編號不可為空" ) );
         return;
      }
      //是否是系統認可的保證書編號....--->
      if($this->valid_warranty_no($warranty_no)==FALSE)
      {
         $this->splash( "failed", $url, app::get( "yswarranty" )->_( "無效的保證書編號" ) );
      }
      if(empty($_POST['purchase_date']))
      {
         $this->splash( "failed", $url, app::get( "yswarranty" )->_( "購買日期不可為空" ) );
      }
      if($_POST['purchase_date']!=$_POST['purchase_date2'])
      {
         $this->splash( "failed", $url, app::get( "yswarranty" )->_( "購買日期不一致" ) );
         return;
      }
   }
   public function updatereg($app = "", $ctl = "", $act = "")
   {

   }
   function copyPOST_to_pagedata()
   {
      if($_POST['reg_id'])
         $this->pagedata['reg_id']=$_POST['reg_id'];//id
      
      $this->pagedata['warranty_no']=trim($_POST['warranty_no']);
      $this->pagedata['purchase_date']=$_POST['purchase_date'];//購買日期
      $this->pagedata['purchase_date']=$_POST['purchase_date'];//購買日期
      $this->pagedata['purchase_date2']=$_POST['purchase_date2'];//購買日期2
      $this->pagedata['product_style']=trim($_POST['product_style']);//機種
      $this->pagedata['product_no']=trim($_POST['product_no']);//機號
      $this->pagedata['warranty_no']=trim($_POST['warranty_no']);//保證書編號
      $this->pagedata['channel']=$_POST['channel'];//購買方式
      $this->pagedata['channelother']=$_POST['channelother'];//購買方式其他
      $this->pagedata['verify_code']=trim($_POST['verify_code']);//保證書驗證碼
      $this->pagedata['reseller_name']=trim($_POST['reseller_name']);//原購經銷商店名
      $this->pagedata['reseller_region']=$_POST['reseller_region'];//原購經銷商地區
      $this->pagedata['reseller_address']=trim($_POST['reseller_address']);//原購經銷商地址
      $this->pagedata['channelopt']=$this->get_channelopt();
   }
   public function toreg($app = "", $ctl = "", $act = "")
   {
      $url = $this->gen_url( array(
                  "app" => "yswarranty",
                  "ctl" => "site_mywarranty",
                  "act" => "newreg"	) );
      //保證書編號 跟保證書驗證碼 不可以都為空
      $this->checkBeforePost($url);
      
      $this->check_warranty_input();

      $warranty_no=trim($_POST['warranty_no']);
      if($this->warranty_no_regiostered($warranty_no))
      {
         $this->splash( "failed", $url, app::get( "yswarranty" )->_( "保證書編號已存在" ) );
      }
      $reg_id=$this->register_warranty();

      if($reg_id==FALSE)
      {
         $this->splash( "failed", $url, app::get( "yswarranty" )->_( "新增資料失敗" ) );
      }
      $this->copyPOST_to_pagedata();
      $this->action_view = "newreg.html";
      $this->output("yswarranty");
   }
   function register_warranty(){

      $member = $this->get_current_member();

      $data=array(
                  "member_id"=>$member['member_id'],
                  "product_style"=>$_POST['product_style'],
                  "product_no"=>$_POST['product_no'],
                  "warranty_no"=>trim($_POST['warranty_no']),
                  "purchase_date"=>strtotime($_POST['purchase_date']),
                  "channel"=>$_POST['channel'],
                  "other_channel"=>$_POST['channelother'],
                  "verify_code"=>$_POST['verify_code'],
                  "reseller_name"=>$_POST['reseller_name'],
                  "reseller_region"=>$_POST['reseller_region'],
                  "reseller_address"=>$_POST['reseller_address'],
                  "createat"=>time(),
                  "last_modified"=>time(),
                  "is_locked"=>'N'
      );

      $warranty_model = app::get( "yswarranty" )->model('warranty_reg');
      $data['other']="test";
      $reg_id=$warranty_model->insert($data);
      return $reg_id;
   }


   function warranty_no_regiostered($warranty_no)
   {
      $filter=array('warranty_no'=>$warranty_no);
      $warranty_model = app::get( "yswarranty" )->model('warranty_reg');
      $count=$warranty_model->count($filter);
      if($count==0)
         return false;
      return true;
   }

   public function newreg($app = "", $ctl = "", $act = "")
   {
      //機種 -->本頁進入點可能是機種選擇頁
      if($_POST['product_style'])
      {
         $this->pagedata['product_style']=$_POST['product_style'];
      }else
         $this->pagedata['product_style']="";
      //保證書編號
      $this->pagedata['warranty_no']="";
      //購買日期
      $this->pagedata['purchase_date']=time();
      $this->pagedata['purchase_date2']=$this->pagedata['purchase_date'];
      //購買方式

      $this->pagedata['channelopt']=$this->get_channelopt();
      //其他
      $this->pagedata['channelother']="";
      //保證書驗證碼
      $this->pagedata['verify_code']="";

      //原經銷商店名
      $this->pagedata['reseller']="";
      //原經銷商區域
      $this->pagedata['reseller_region']="";
      //原經銷商地址
      $this->pagedata['reseller_address']="";
      $this->pagedata['smalltitle']=app::get("yswarranty")->_("商品註冊");      
      $this->output("yswarranty");
   }
   public function gen_askvcode( )
   {
      $vcode = kernel::single( "base_vcode" );
      $vcode->length( 4 );
      $vcode->verify_key( "ASKVCODE" );
      $vcode->display( );
   }
   public function vcodecheck( )
   {
      $name = trim( $_POST['name'] );
      if ( !base_vcode::verify( "ASKVCODE", intval( $name ) ) )
      {
         echo "<span class=\"fontcolorRed\">&nbsp;".app::get( "b2c" )->_( "验证码填写错误,请重新输入" )."</span>";
         exit( );
      }
   }
   public function editwarranty()
   {
      $url = $this->gen_url( array(
                  "app" => "yswarranty",
                  "ctl" => "site_mywarranty",
                  "act" => "listwarranties"	) );

      if(empty($_GET['id']))
      {
         $this->splash( "failed", $url, app::get( "yswarranty" )->_( "無此紀錄" ) );
      }

      $reg_id=$_GET['id'];
      $warranty_reg=app::get("yswarranty")->model("warranty_reg");
      $filter=array("reg_id"=>$reg_id);
      $data=$warranty_reg->getList(' * ',$filter,0,1);

      if(count($data)==0)
      {
         $this->splash( "failed", "back", app::get( "yswarranty" )->_( "無此紀錄" ) );
      }
      if($data[is_locked]=='Y')
      {
         $this->splash( "failed", "back", app::get( "yswarranty" )->_( "本紀錄無法編輯" ) );
      }
      $this->pagedata=array_merge($this->pagedata,$data[0]);

      $this->pagedata['purchase_date2']=$data[0]['purchase_date'];

      $this->pagedata['channelopt']=$this->get_channelopt();
      $this->pagedata['warrantyedit']='Y';
      $this->action_view = "newreg.html";
      $this->pagedata['smalltitle']=app::get("yswarranty")->_("商品註冊資料修改");
      $this->output("yswarranty");
   }
   function getEditLinkContent($id)
   {
      $url = $this->gen_url( array(
                  "app" => "yswarranty",
                  "ctl" => "site_mywarranty",
                  "act" => "editwarranty"	) );
      return '<a href="'.$url.'?id='.$id.'"><img src="'.kernel::base_url( ).'/app/yswarranty/statics/edit.png"></a>';

   }
   function getDelLinkContent($id)
   {
      $c="<img class=\"imgbundle\" app=\"yswarranty\" onclick=\"delgoods(".$id.")\" style=\"cursor: pointer;\" title=\"删除\" src=\"".kernel::base_url( )."/app/yswarranty/statics/del.png\">";
      return $c;
   }
   function listwarranties()
   {
      //1.分頁
      $page = ( integer )$this->_request->get_param( 1 );
      if(empty($page ))
      {
         $page=1;
      }
      $pageLimit=20;

      $warranty_reg=app::get("yswarranty")->model("warranty_reg");

      $member = $this->get_current_member();
      $filter=array('member_id'=>$member['member_id']);
      //1.getcount
      $count=$warranty_reg->count($filter);
      $totalpage=ceil( $count / $pageLimit );

      $fields="reg_id,purchase_date,product_style,product_no,warranty_no,reseller_name,is_locked";
      $aDatas=$warranty_reg->getList($fields,$filter,($page-1)*$pageLimit,$pageLimit);


      foreach($aDatas as $k=>$item)
      {
         if($aDatas[$k]['is_locked']=='Y')
         {
            $aDatas[$k]['edit']='';
            $aDatas[$k]['delete']='';
         }
         else
         {
            $aDatas[$k]['edit']=$this->getEditLinkContent($item['reg_id']);
            $aDatas[$k]['delete']=$this->getDelLinkContent($item['reg_id']);
         }
      }

      $this->pagedata['warranties']=$aDatas;

      $this->pagedata['count']=$count;


      $token = md5( "page{$page}" );
      $this->pagedata['pager'] = array(
                  "current" => $page,
                  "total" => $totalpage,
                  "link" => $this->gen_url( array(
                              "app" => "yswarranty",
                              "ctl" => "site_mywarranty",
                              "act" => "listwarranties",
                              "arg2" => $token
                  ) ),
                  "token" => $token
      );
      $this->output("yswarranty");
      //$this->page( "site/warranty/showlist.html" );
   }
}
?>